

function customLootTable(LootTable) {
    /**@type {{count:{max:Number,min:Number},entries:[{item:String,count:{max:Number,min:Number},weight:Number}]}}*/
    this.LootTable = JSON.parse(JSON.stringify(LootTable))

}

customLootTable.prototype.giveLoot = function (/**@type {Internal.Player}*/player) {

    /**@type {Number}*/
    var count = getCount(this.LootTable.count)
    count = Math.min(count, this.LootTable.entries.length)

    for (let index = 0; index < count; index++) {
        var index_ = getExtract(this.LootTable.entries)
        var entrie = this.LootTable.entries.splice(index_, 1)[0]

        player.give(Item.of(entrie.item).withCount(getCount(entrie.count)))
    }
}


const customNoRepeatLootTables = {
    test: {
        count: {
            max: 1,
            min: 1
        },
        entries: [
            {
                item: "minecraft:iron_ingot",
                count: {
                    max: 1,
                    min: 1
                },
                weight: 5
            },
            {
                item: "minecraft:diamond",
                count: {
                    max: 1,
                    min: 1
                },
                weight: 5
            }
        ]
    }
}

function getExtract(/**@type {[{item:String,count:{max:Number,min:Number},weight:Number}]}*/entries) {
    var totalWeight = 0
    entries.forEach(entrie => {
        totalWeight = totalWeight + entrie.weight
    })
    var random = totalWeight * Math.random()


    for (let index = 0; index < entries.length; index++) {
        var entrie = entries[index];
        if (entrie.weight < random) {
            random = random - entrie.weight
        } else {
            return index
        }
    }

}

/**@returns {Number}*/
function getCount(count) {
    if (count instanceof Number) {
        return count
    } else {
        return (count.min + (count.max - count.min) * Math.random()).toFixed(0)
    }
}